using System.Collections.Generic;
using System.Threading.Tasks;
using UnityEngine;

namespace Light
{
    /// <summary>
    ///     ����Item �ĸ��ֻ�ȡ��ȫ����ȡ��������ѯ�����ֶ���
    /// </summary>
    public class ItemManager : Singleton<ItemManager>, ISingleton
    {
        private int curRandomWeaponIndex;

        protected int CurRandomWeaponIndex
        {
            get => curRandomWeaponIndex;
            set
            {
                if (value >= weaponNum)
                    curRandomWeaponIndex = value - weaponNum;
                else
                    curRandomWeaponIndex = value;
            }
        }

        private int weaponNum { get; set; }


        //�ö���ĳ�ʼ��
        public void Initialize()
        {
        }

        /// <summary>
        ///     ��ȡ������Goods���д�ŵ��������ݣ�ֻ��
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<AbstractItemSO> GetGoodsItemsData()
        {
            List<AbstractItemSO> res = new();
            var items = ItemsTable.Instance.GetData<WeaponItemListSO, WeaponItemSO>(ItemType.Weapon);
            var items1 = ItemsTable.Instance.GetData<PropItemListSO, PropItemSO>(ItemType.Prop);
            foreach (var data in ItemsTable.Instance.GetGoodsItemJsonData())
            {
                var tempData = items.Find(item => item.id == data.id.ToString());
                if (tempData != null) res.Add(tempData);

                var tempData1 = items1.Find(item => item.id == data.id.ToString());
                if (tempData1 != null) res.Add(tempData1);
            }

            return res;
        }

        /// <summary>
        ///     ��ȡ�Ѿ�װ����Item���ݣ�ֻ��
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<AbstractItemSO> GetGoodItemsEquipData()
        {
            List<AbstractItemSO> res = new();
            var items = ItemsTable.Instance.GetData<WeaponItemListSO, WeaponItemSO>(ItemType.Weapon);
            var items1 = ItemsTable.Instance.GetData<PropItemListSO, PropItemSO>(ItemType.Prop);
            foreach (var data in ItemsTable.Instance.GetGoodsItemEquipJsonData())
            {
                var tempData = items.Find(item => item.id == data.id.ToString());
                if (tempData != null) res.Add(tempData);

                var tempData1 = items1.Find(item => item.id == data.id.ToString());
                if (tempData1 != null) res.Add(tempData1);
            }

            return res;
        }

        /// <summary>
        ///     ��ȡEquip��Item��JsonData
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<GoodsJsonData> GetItemsEquipJsonDataAsync()
        {
            return ItemsTable.Instance.GetGoodsItemEquipJsonDataAsync();
        }

        /// <summary>
        ///     ��ȡItemJsonData
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<GoodsJsonData> GetItemsJsonDataAsync()
        {
            return ItemsTable.Instance.GetGoodsItemJsonData();
        }

        /// <summary>
        ///     �첽�汾
        /// </summary>
        /// <returns></returns>
        public static async Task<IEnumerable<AbstractItemSO>> GetGoodItemsEquipDataAsync()
        {
            List<AbstractItemSO> res = new();
            var items = await ItemsTable.Instance.GetDataAsync<WeaponItemListSO, WeaponItemSO>(ItemType.Weapon);
            var items1 = await ItemsTable.Instance.GetDataAsync<PropItemListSO, PropItemSO>(ItemType.Prop);
            foreach (var data in ItemsTable.Instance.GetGoodsItemEquipJsonData())
            {
                var tempData = items.Find(item => item.id == data.id.ToString());
                if (tempData != null) res.Add(tempData);

                var tempData1 = items1.Find(item => item.id == data.id.ToString());
                if (tempData1 != null) res.Add(tempData1);
            }

            return res;
        }

        /// <summary>
        ///     �ڽ������е�Item����װ�����߲�ж����ʱ��ʹ��
        /// </summary>
        /// <param name="origin"></param>
        /// <param name="target"></param>
        /// <param name="item"></param>
        public static void MoveJsonData(FunctionBarContentType origin, FunctionBarContentType target,
            AbstractItemSO item)
        {
            //��������
            var data = ItemsTable.Instance.FindJsonDataById(int.Parse(item.id), origin);

            //�ֱ�ִ��ɾ�������Ӳ���
            if (origin == FunctionBarContentType.GoodsScrollView)
            {
                ItemsTable.Instance.DeleteJsonDataById(data.id);
                ItemsTable.Instance.AddJsonDataToEquipData(data);
            }
            else if (origin == FunctionBarContentType.GoodsItemEuqip)
            {
                ItemsTable.Instance.DeleteJsonDataEquipById(data.id);
                ItemsTable.Instance.AddJsonDataToData(data);
            }
        }

        /// <summary>
        ///     �򱳰���װ����Item
        /// </summary>
        /// <param name="id"></param>
        public static void AddItemToJsonDataById(int id)
        {
            ItemsTable.Instance.AddJsonDataToDataByItemId(id);
        }

        /// <summary>
        ///     ���ճ�����item���ݲ����ӵ�json��
        /// </summary>
        /// <param name="list"></param>
        public static void AddItemListToJsonDataByItems(List<AbstractItemSO> list)
        {
            foreach (var item in list) ItemsTable.Instance.AddJsonDataToDataByItemId(int.Parse(item.id));
        }


        /// <summary>
        ///     ��װ��������������
        /// </summary>
        /// <param name="data"></param>
        public static void AddNewJsonDataToEquipData(GoodsJsonData data)
        {
            ItemsTable.Instance.AddJsonDataToEquipData(data);
        }

        public static void AddNewJsonDataToData(GoodsJsonData data)
        {
            ItemsTable.Instance.AddJsonDataToData(data);
        }

        /// <summary>
        ///     ͨ��ItemType��ȡ�����ָ��������Item��Ĭ����һ��
        /// </summary>
        /// <param name="type"></param>
        /// <param name="count"></param>
        /// <returns></returns>
        public static List<AbstractItemSO> GetRandomItemByItemType(ItemType type, int count = 1)
        {
            //��ʼ���������
            Random.InitState((int)Time.time);
            List<AbstractItemSO> res = new();

            if (type == ItemType.Weapon) Instance.weaponNum = ItemsTable.Instance.GetWeaponCount();
            for (var i = 0; i < count; ++i)
            {
                var id = Random.Range(1, Instance.weaponNum);
                Instance.CurRandomWeaponIndex += id;
                res.Add(ItemsTable.Instance.GetItemByTypeAndIndex(type, Instance.CurRandomWeaponIndex));
            }

            return res;
        }

        /// <summary>
        ///     ɾ�����ݣ�ȫ����һ�飬��̨�м��
        /// </summary>
        /// <param name="id"></param>
        public static void DeleteItem(int id)
        {
            ItemsTable.Instance.DeleteJsonDataById(id);
            ItemsTable.Instance.DeleteJsonDataEquipById(id);
        }
    }
}